﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogic;
using System.Data;
using DataAccess;
using Config;

namespace Manager
{
    public partial class NewsManager : System.Web.UI.Page
    {
        NewsBusiness newsBusiness = new NewsBusiness();

        private int CateID
        {
            get
            {
                try
                {
                    return Int32.Parse(Request.QueryString["CatID"]);
                }
                catch (System.Exception)
                {
                    return -1;
                }
            }
        }

        private int UserID
        {
            get
            {
                try
                {
                    if (Request.QueryString["UserID"] != null)
                    {
                        return Int32.Parse(Request.QueryString["UserID"]);
                    } 
                    else
                    {
                        return Int32.Parse(Session[Config.Constants.SESSION_USERID].ToString());
                    }
                }
                catch (System.Exception)
                {
                    return -1;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ucPaging.PageChange += new global::Manager.Controls.ucPaging.PagingHandler(Load_Data);
            if (!Page.IsPostBack)
            {
            }
            ucPaging.PageDisplay = 5;
            ucPaging.PageSize = 10;
            Load_Data();
            ucPaging.Visible = ucPaging.TotalPage > 1;
        }

        private void Load_Data()
        {
            int totalRecord = 0;
            string text = txtSearch.Text.Trim();
            DataTable dtbNews = newsBusiness.SearchNews(text,  -1, ucPaging.CurrentPage, ucPaging.PageSize, ref totalRecord);
            ucPaging.TotalRecord = totalRecord;

            if (dtbNews.Rows.Count > 0)
            {
                rptNews.DataSource = dtbNews;
                rptNews.DataBind();
            }
            else
            {
                noNews.Visible = true;
                rptNews.Visible = false;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Load_Data();
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddNews.aspx");
        }

        protected void rptNews_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            int newsID = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "NewsID"));
            Label lblStatus = (Label)e.Item.FindControl("lblStatus");
            Label lblDescription = (Label)e.Item.FindControl("lblDescription");

            NewsInfo newsInfo = NewsData.SelectByNewsID(newsID);
            string description = newsInfo.NewsDescription;
            if (description.Length > 200)
            {
                lblDescription.Text = description.Substring(0, 200) + "...";
            } 
            else
            {
                lblDescription.Text = description;
            }

            if (newsInfo.NewsStatus == true)
            {
                lblStatus.Text = "Hiển thị";
            } 
            else
            {
                lblStatus.Text = "Không Hiển thị";
            }
        }

        protected void rptNews_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int newsID = Convert.ToInt32(e.CommandArgument);

            if (e.CommandName.Equals("View"))
            {
                Response.Redirect("ViewNews.aspx?NewsID=" + newsID);
            }

            if (e.CommandName.Equals("Del"))
            {
                NewsData.DeleteByNewsID(newsID);
                Response.Redirect("NewsManager.aspx");
            }

            if (e.CommandName.Equals("Lock"))
            {
                newsBusiness.UpdateStatus(newsID);
                Response.Redirect("NewsManager.aspx");
            }
        }
    }
}